﻿define(['admin/app', 'service/admin/shopInfoService'], function (app) {
    "use strict";
    app.controller('shopInfoController', function ($scope, $state, $stateParams, $location, utils, w5cValidator, shopInfoService, baseService) {
        $scope.list = {};
        $scope.query = { page: 1, size: 20 };
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                shopInfoService.gets($scope.query).success(function (response) { $scope.model = response.data; });
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            edit: function () {
                shopInfoService.get({ id: $stateParams.id }).success(function (response) {
                    $scope.model = response.data;
                    $scope.isEdit = $stateParams.isEdit
                });
                $scope.breadcrumb = $stateParams.isEdit == 1 ? "编辑":"商家详情";

            },
            enable: function (model, status) {
                var modal = utils.confirm({ msg: status == 1 ? "您确定要冻结该商家么" : "您确定要解冻该商家么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    shopInfoService.enable(model, status).success(function () {
                        methods.search();
                    });
                });
            },
            save: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                shopInfoService.updateShopInfo(model).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.goBack();
                    else
                        $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            goBack: function () {
                history.back();
            }
        };

        angular.extend($scope, methods);
        if ($state.includes('app.shopinfo.detail')) {
            methods.edit();
        } else {
            methods.search();
        }
    });
});